package com.sync.syncmasterdata;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sync.syncmasterdata.configuration.ConfInfo;
import com.sync.syncmasterdata.customer.service.http.HttpAPIServiceForCrmSync;
import com.sync.syncmasterdata.product.controller.ProductController;
import com.sync.syncmasterdata.product.pojo.cp.InventoryForCP;
import com.sync.syncmasterdata.product.pojo.erp.InventoryForU8;
import com.sync.syncmasterdata.product.pojo.oa.InventoryForOA;
import com.sync.syncmasterdata.product.service.SyncInventoryService;
import com.sync.syncmasterdata.product.service.convert.ProductOAToU8;
import com.sync.syncmasterdata.util.AuthUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

@SpringBootTest
@Slf4j
class SyncMasterDataApplicationTestsU8EAI {

    /**
     * CP get token 测试
     */
    private String APIURL = "http://172.16.0.7:807/U8EAI/import.asp";

    /**
     * 创建物料 产品
     * 创建服务使用 u8的eai接口
     *
     * @param entity
     */
    private void createProduct(ProduceProductDTO entity) {

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        String requestXml = pushXmlMsg(entity);
        String ret = restTemplate.postForObject(APIURL, requestXml, String.class);
//        String retU = new String(ret.getBytes("ISO-8859-1"),"UTF-8");
        log.info("打印---->请求606U8新增物料(产品信息)后返回结果---->{}", ret);
    }

    //推送U8的xml 格式处理
    //推送U8的xml 格式处理
    private String pushXmlMsg(ProduceProductDTO entity) {
        String xmlHead = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?><ufinterface roottag=\"inventory\" receiver=\"u8\" sender=\"606\" proc=\"add\" codeexchanged=\"N\" exportneedexch=\"N\" version=\"2.0\">";
        String xmlBody = xmlHead + "<inventory><header>%s</header><body></body></inventory></ufinterface>";
        //前端入参
        String xmlEntity = "<code>" + entity.getCInvCode() + "</code>" +
                "<name>" + entity.getCInvName() + "</name>" +
                "<sort_code>" + entity.getCInvCCode() + "</sort_code>" +
                "<main_measure>" + entity.getCComUnitCode() + "</main_measure>" +
                "<specs>" + entity.getCInvStd() + "</specs>" +
                "<self_define2>" + entity.getCInvDefine2() + "</self_define2>" +
                "<self_define3>" + entity.getCInvDefine3() + "</self_define3>" +
                "<self_define6>" + entity.getCInvDefine6() + "</self_define6>" +
                "<cMassUnit>" + entity.getCMassUnit() + "</cMassUnit>" +
                "<iMassDate>" + entity.getIMassDate() + "</iMassDate>";
        //变量固定
        xmlEntity += "<inv_position>" + entity.getCPosition() + "</inv_position>" +
                "<cshopunit>" + entity.getCShopUnit() + "</cshopunit>" +
                "<sale_flag>" + entity.getBSale() + "</sale_flag>" +
                "<purchase_flag>" + entity.getBPurchase() + "</purchase_flag>" +
                "<selfmake_flag>" + entity.getBSelf() + "</selfmake_flag>" +
                "<prod_consu_flag>" + entity.getBComsume() + "</prod_consu_flag>" +
                "<in_making_flag>" + entity.getBProducing() + "</in_making_flag>" +
                "<tax_serv_flag>" + entity.getBService() + "</tax_serv_flag>" +
                "<suit_flag>" + entity.getBAccessary() + "</suit_flag>" +
                "<tax_rate>" + entity.getITaxRate() + "</tax_rate>" +
                "<bexpsale>" + entity.getBExpSale() + "</bexpsale>" +
                "<cPlanMethod>" + entity.getCPlanMethod() + "</cPlanMethod>" +
                "<cSRPolicy>" + entity.getCSRPolicy() + "</cSRPolicy>" +
                "<bequipment>" + entity.getBEquipment() + "</bequipment>" +
                "<bProxyForeign>" + entity.getBProxyForeign() + "</bProxyForeign>" +
                "<unit_volume>" + entity.getIVolume() + "</unit_volume>" +
                "<advance_period>" + entity.getIInvAdvance() + "</advance_period>" +
                "<qlty_guarantee_flag>" + entity.getBInvQuality() + "</qlty_guarantee_flag>" +
                "<batch_flag>" + entity.getBInvBatch() + "</batch_flag>" +
                "<entrust_flag>" + entity.getBInvEntrust() + "</entrust_flag>" +
                "<backlog_flag>" + entity.getBInvOverStock() + "</backlog_flag>" +
                "<bInTotalCost>" + entity.getBInTotalCost() + "</bInTotalCost>" +
                "<fConvertRate>" + entity.getFConvertRate() + "</fConvertRate>" +
                "<bbommain>" + entity.getBBomMain() + "</bbommain>" +
                "<bbomsub>" + entity.getBBomSub() + "</bbomsub>" +
                "<iPlanDefault>" + entity.getIPlanDefault() + "</iPlanDefault>" +
                "<iAllocatePrintDgt>" + entity.getIAllocatePrintDgt() + "</iAllocatePrintDgt>" +
                "<defwarehouse>" + entity.getCDefWareHouse() + "</defwarehouse>" +
                "<unitgroup_code>" + entity.getCGroupCode() + "</unitgroup_code>";
        //替换
        xmlBody = String.format(xmlBody, xmlEntity);
        log.info("打印---->请求606U8新增物料(产品信息)前拼接的XML字符串---->{}", xmlBody);
        return xmlBody;
    }

    @Test
    public void runFunction() {
        ProduceProductDTO entity = new ProduceProductDTO();
        entity.setCInvCCode("00RG00");
        entity.setCInvCode("RG09999");
        entity.setCInvName("RG09999--测试");
        entity.setCInvStd("测试型号");
        entity.setCComUnitCode("001");
        entity.setIMassDate(99);
        entity.setCMassUnit(2);
        entity.setCInvDefine2("setCInvDefine2");
        entity.setCInvDefine3("setCInvDefine3");
        entity.setCInvDefine6("setCInvDefine6");

        createProduct(entity);

    }
}

@Data
class ProduceProductDTO {
    //必须传入的变量 遵循之前的杰毅OA的sql插入开发
    //编码
    private String cInvCode;
    //名称
    private String cInvName;
    //存货分类
    private String cInvCCode;
    //规格
    private String cInvStd;
    //单位 编码
    private String cComUnitCode;
    private String cInvDefine2;
    private String cInvDefine3;
    private String cInvDefine6;
    //保质期日期 整型
    private Integer iMassDate;
    //保质期 单位 整型(字典)
    private Integer cMassUnit;
//---------------------分隔线--------------

    //固定值+开关b"开头的" 遵循之前的杰毅OA的sql插入开发
    private String cPosition = "02000";
    //零售计量单位
    private String cShopUnit = "027";
    private Integer bSale = 1;
    private Integer bPurchase = 1;
    //是否资质
    private Integer bSelf = 0;
    //是否生产耗用
    private Integer bComsume = 1;
    //是否在制
    private Integer bProducing = 0;
    //是否应税劳务
    private Integer bService = 0;
    //是否成套
    private Integer bAccessary = 0;
    //税率
    private Integer iTaxRate = 0;
    //外销
    private Integer bExpSale = 1;
    //计划方法
    private String cPlanMethod = "R";
    //供需政策
    private String cSRPolicy = "PE";
    //是否设备
    private Integer bEquipment = 1;
    //是否委外
    private Integer bProxyForeign = 1;
    //单位体积
    private Integer iVolume = 0;
    //提前期
    private Integer iInvAdvance = 0;
    //是否保质期 管理
    private Integer bInvQuality = 1;
    //是否批次管理
    private Integer bInvBatch = 1;
    //是否受托代销
    private Integer bInvEntrust = 0;
    //是否呆滞积压
    private Integer bInvOverStock = 0;
    //是否累计成本
    private Integer bInTotalCost = 1;
    //转换因子
    private Integer fConvertRate = 1;
    private Integer bBomMain = 1;
    private Integer bBomSub = 1;
    //默认计划属性
    private Integer iPlanDefault = 3;
    //子件用料打印精度
    private Integer iAllocatePrintDgt = 4;
    //仓库
    private String cDefWareHouse = "01";
    //计量组件编码
    private String cGroupCode = "01";

}
